<template>
    <div>
        <!-- 面包屑导航区域 -->
    <el-card class="box-card">
  <!-- 搜索与添加区域 -->
        <el-row :gutter="20">
          <el-col :span="9">
            <el-input placeholder="请输入学号" v-model="queryInfo.stuNumber" clearable @clear="getUserList">
              <el-button slot="append" icon="el-icon-search"
               @click="getUserList"></el-button>
            </el-input>
  </el-col>
  <el-col :span="4">
      <el-button type = "primary" @click="addDialogVisible = true">添加用户</el-button>
  </el-col>

</el-row>

   <!-- 用户列表区域 -->
    <el-table  class="user" :data="userList" border stripe>
       <el-table-column type = "index" label="#" prop=""></el-table-column>
       <el-table-column label="姓名" prop="username"></el-table-column>
       <el-table-column label="性别" prop="gender"></el-table-column>
       <el-table-column label="学号" prop="stuNumber"></el-table-column>
       <el-table-column label="年级" prop="grade"></el-table-column>
       <el-table-column label="学院" prop="college"></el-table-column>

      <el-table-column label="操作" width="210">
        <template slot-scope="scope">
           <el-tooltip  effect="dark" content="修改用户信息" placement="top" :enterable = "false">
       <!-- 修改按钮 -->
            <el-button
              type="primary"
              class="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
         </el-tooltip>

           <el-tooltip class="item" effect="dark" content="删除用户" placement="top" :enterable = "false">
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              class="el-icon-delete"
              size="mini"
              @click="removeUserById(scope.row.id)"
            ></el-button>
          </el-tooltip>
          </template>
      </el-table-column>

      </el-table>
      <!-- 用户列表分页区域 -->
     <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.page"
      :page-size="queryInfo.size"
      layout="total, prev, pager, next, jumper"
      :total="total">
      </el-pagination>
    </el-card>

    <!-- 添加用户的对话框 -->
    <el-dialog
  title="添加用户"
  :visible.sync="addDialogVisible"
  width="30%"
  @close="addDialogClosed"
  >
  <!-- 内容主体区域 -->
  <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
  <el-form-item label="姓名" prop="username">
    <el-input v-model="addForm.username"></el-input>
  </el-form-item>
   <el-form-item label="密码" prop="password">
    <el-input v-model="addForm.password"></el-input>
  </el-form-item>
   <el-form-item label="性别" prop="gender">
    <el-input v-model="addForm.gender"></el-input>
  </el-form-item>
  <el-form-item label="学号" prop="stuNumber">
    <el-input v-model="addForm.stuNumber"></el-input>
  </el-form-item>
  <el-form-item label="年级" prop="grade">
    <el-input v-model="addForm.grade"></el-input>
  </el-form-item>
  <el-form-item label="学院" prop="college">
    <el-input v-model="addForm.college"></el-input>
  </el-form-item>
  </el-form>
  <!-- 底部区域 -->
  <span slot="footer" class="dialog-footer">
    <el-button @click="addDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addUser">确 定</el-button>
  </span>
</el-dialog>
<!-- 修改用户的对话框 -->
<el-dialog
  title="修改用户信息"
  :visible.sync="editDialogVisible"
  width="50%" @close = "editDialogClosed">
   <el-form :model="updateUser" :rules="editFormRules" ref="editFormRef" label-width="70px">
  <el-form-item label="学号" prop="gender">
    <el-input v-model="updateUser.stuNumber"></el-input>
  </el-form-item>
  <el-form-item label="年级" prop="grade">
    <el-input v-model="updateUser.grade"></el-input>
  </el-form-item>
  <el-form-item label="学院" prop="college">
    <el-input v-model="updateUser.college"></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="editDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editUserInfo">确 定</el-button>
  </span>
</el-dialog>
 </div>
</template>

<script>

export default {
   data() {
    return {
      //获取用户列表参数对象
      queryInfo:
      {
        stuNumber:'',
        //当前的页数
        page:'1',
        //显示多少数量
        size:'5'
      },
      userList:[],
      total:0,
      tableData:[{test:'123456'},{test:'123'}],
      formlist: {

      },
      // 控制修改用户对话框的显示与隐藏
      editDialogVisible: false,
      addDialogVisible: false,
    //  添加用户的表单数据
     addForm:{
        username:'',
        password:'',
        gender:'',
        stuNumber:'',
        grade:'',
        college:''
     },
      deleteUser:{
        id:''
     },
       updateUser:{
        id:'',
        stuNumber:'',
         grade:'',
        college:''
     },
     editForm:{
        stuNumber:'',
        grade:'',
        college:''
     },

    // 添加表单的验证规则
     addFormRules:{
         username:[
             {
                 required:true,message:'请输入姓名',trigger:'blur'
             },
         ],
          password:[
             {
                 required:true,message:'请输入密码',trigger:'blur'
             }
         ],
         gender:[
             {
                 required:true,message:'请输入性别',trigger:'blur'
             }
         ],
         stuNumber:[
             {
                 required:true,message:'请输入学号',trigger:'blur'
             }
         ],
         grade:[
             {
                  required:true,message:'请输入年级',trigger:'blur'
             }
         ],
         college:[
             {
                 required:true,message:'请输入学院',trigger:'blur'
             }
         ]
     }
    }
  },
  //页面创建期间就会调用这个函数
  created()
  {
    this.getUserList()
  },
  methods:
  {
    //   监听添加用户对话框的关闭事件
      addDialogClosed()
      {
          this.$refs.addFormRef.resetFields()
      },
       //   监听修改用户对话框的关闭事件
      editDialogClosed()
      {
          this.$refs.editFormRef.resetFields();
      },
      // 展示编辑用户的对话框
      async showEditDialog(id)
      {
        console.log(id)
         this.deleteUser.id = id;
         const {data : res} = await this.$http.get('http://113.118.193.85:17088/user/selectUserById',{params:this.deleteUser});
         if(res.status !== 200)
         {
           return this.$message.error('查询用户信息失败')
         }
         this.updateUser = res.data
         this.editDialogVisible = true
      },
      async getUserList()
      {
        const {data:res} = await this.$http.get('http://113.118.193.85:17088/user/userList',{params:this.queryInfo});
        if(res.status !== 200)
        {
           return this.$message.error('获取用户列表失败');
        }
        this.userList = res.data.list;
        this.total = res.data.total;
      },
      //监听 pagesize改变的事件
      handleSizeChange(newSize)
      {
        this.queryInfo.size = newSize;
        this.getUserList();
      },
      //监听 页码值改变的事件
      handleCurrentChange(newPage)
      {
        this.queryInfo.page = newPage;
         this.getUserList();
      },
      //点击按钮，添加新用户
       addUser(){

        //添加添加之前先进行表单预校验
         this.$refs.addFormRef.validate(async (valid) => {
            if (!valid) return;
           const {data : res} =  await this.$http.post('http://113.118.193.85:17088/user/userAdd',this.addForm);
           if(res.status !== 200)
            {
              return this.$message.error('添加用户失败!');
            }
            this.$message.success('添加用户成功!');
           this.getUserList();
            //隐藏添加用户的对话框
            this.addDialogVisible = false;
         })
       },
       //根据Id删除对应的用户信息
       async removeUserById(id)
       {

         this.deleteUser.id = id;
        //弹框提示
        const confirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
        ).catch(err => {
          return err;
        })
        if(confirmResult !== 'confirm')
        {
          return this.$message.info('已经取消删除')
        }

        const {data : res} = await this.$http.post('http://113.118.193.85:17088/user/deleteUser',this.deleteUser);

        if(res.status !== 200)
        {
          return this.$message.error('删除用户失败')
        }

        this.$message.success('删除用户成功！')
        this.getUserList();
       },
       async editUserInfo()
       {


           //发起修改用户数据的请求
            const {data : res} = await this.$http.post('http://113.118.193.85:17088/user/updateUser',this.updateUser);
            if(res.status !== 200)
            {
              return this.$message.error('修改用户失败')
            }

            //关闭对话框
            this.editDialogVisible = false;
            //刷新用户数据列表
            this.getUserList();
            //提示修改成功
             this.$message.success('修改用户成功')
         }
       }

       }

</script>

<style>
.user
{
    margin-top: 15px;
}
</style>
